package com.gking.resourcePool.common;

import java.util.Map;

/**
 * 基于ThreadLocal封装工具类，用户保持和获取当前登录用户 id
 */
public class BaseContext {

    // 用于存储当前用户id
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    /**
     * 设置值
     *
     * @param id
     */
    public static void setCurrentId(String id) {
        threadLocal.set(id);
    }

    /**
     * 获取值
     *
     * @return
     */
    public static String getCurrentId() {
        return threadLocal.get();
    }


    //  用于保存请求头
    private static ThreadLocal<Map<String, String>> threadLocalTwo = new ThreadLocal<>();

    public static void setHeader(Map<String, String> headers) {
        threadLocalTwo.set(headers);
    }

    public static Map<String, String> getHeader() {
        return threadLocalTwo.get();
    }


    //  存储响应信息
    private static ThreadLocal<String> threadLocalThree = new ThreadLocal<>();

    public static void setResponseMessage(String message) {
        threadLocalThree.set(message);
    }

    public static String getResponseMessage() {
        return threadLocalThree.get();
    }


    // 存储响应状态
    private static ThreadLocal<Integer> threadLocalFour = new ThreadLocal<>();

    public static void setResponseCode(Integer code) {
        threadLocalFour.set(code);
    }

    public static Integer getResponseCode() {
        return threadLocalFour.get();
    }


    // 存储登录验证码
    private static ThreadLocal<String> threadLocalFive = new ThreadLocal<>();

    public static void setLoginVerifyCode(String code) {
        threadLocalFive.set(code);
    }

    public static String getLoginVerifyCode() {
        return threadLocalFive.get();
    }


    // 销毁存储的数据，以防内存泄漏等问题
    public static void destroy() {
        threadLocal.remove();
        threadLocalTwo.remove();
        threadLocalThree.remove();
        threadLocalFour.remove();
        threadLocalFive.remove();
    }

}
